package com.kevin.springboot.dao;

import com.kevin.springboot.pojo.DepartMent;
import com.kevin.springboot.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname EmployeeDao
 * @Description TODO
 * @Date 2020/6/24 16:54
 * @Created by kevin
 */
public class EmployeeDao {

    //模拟员工数据库
    private static Map<Integer, Employee> employees = null;

    @Autowired
    private DepartMentDao departMentDao;

    static {
        employees = new HashMap<Integer, Employee>();
        employees.put(1001, new Employee(1001, "aaa", "111@sbc.com", 0, new DepartMent(101, "教学部")));
        employees.put(1002, new Employee(1002, "bbb", "222@sbc.com", 1, new DepartMent(102, "后勤部")));
        employees.put(1003, new Employee(1003, "ccc", "333@sbc.com", 1, new DepartMent(103, "计算机部")));
        employees.put(1004, new Employee(1004, "ddd", "444@sbc.com", 0, new DepartMent(104, "科学部")));
        employees.put(1005, new Employee(1005, "eee", "555@sbc.com", 1, new DepartMent(105, "管理部")));

    }

    /**
     * 模拟数据库主键自增
     */
    private static Integer initId = 1006;

    /**
     * 新增员工
     *
     * @param employee
     * @return
     */
    public void save(Employee employee) {
        if (employee.getId() == null) {
            employee.setId(initId++);
        }
        employee.setEmail(employee.getEmail());
        employee.setGender(employee.getGender());
        employee.setDepartMent(departMentDao.getDepartMentById(employee.getDepartMent().getId()));
        employees.put(employee.getId(), employee);
    }

    /**
     * 查询所有的员工
     *
     * @return
     */
    public Collection<Employee> getAllEmployee() {
        return employees.values();
    }

    /**
     * 通过ID查询员工信息
     *
     * @param id
     * @return
     */
    public Employee getEmployeeById(Integer id) {
        return employees.get(id);
    }

    /**
     * 删除员工
     *
     * @param id
     */
    public void deleteEmployeeById(Integer id) {
        employees.remove(id);
    }

}
